<!DOCTYPE html>
<!--在html中引入此命名空间，可避免编辑器出现html验证错误，虽然加不加命名空间对Thymeleaf的功能没有任何影响。-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <!--jquery-->
    <script type="text/javascript" th:src="@{/jquery/jquery-3.6.0.js}"></script>
    <!--bootstrap-->
    <link rel="stylesheet" type="text/css" th:href="@{/bootstrap-3.4.1/bootstrap.css}">
    <script type="text/javascript" th:src="@{/bootstrap-3.4.1/bootstrap.js}"></script>
    <!--jquery-validator-->
    <script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.js}"></script>
    <script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.js}"></script>
    <!--jquery-json-->
    <script type="text/javascript" th:src="@{/jquery/json/jquery.json.min.js}"></script>
    <!--md5-->
    <script type="text/javascript" th:src="@{/md5/md5.js}"></script>
    <!--common-->
    <script type="text/javascript" th:src="@{/js/common.js}"></script>
</head>
<body>
<form name="loginForm" id="loginForm" method="post" style="width: 50%; margin: 0 auto;">
    <h2 style="text-align: center; margin-bottom: 20px">用户登录</h2>
    <div class="form-group">
        <div class="row">
            <label class="form-label col-md-4">请输入手机号码</label>
            <div class="col-md-5">
                <input id="mobile" name="mobile" class="form-control" type="text" placeholder="手机号码" required="true"
                       minlength="11" maxlength="11"/>
            </div>
            <div class="col-md-1">
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <label class="form-label col-md-4">请输入密码</label>
            <div class="col-md-5">
                <input id="password" name="password" class="form-control" type="password" placeholder="密码"
                       required="true" minlength="6" maxlength="16"/>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-5">
            <button class="btn btn-primary btn-block" type="reset" onclick="reset()">重置</button>
        </div>
        <div class="col-md-5">
            <button class="btn btn-primary btn-block" type="submit" onclick="login()">登录</button>
        </div>
    </div>
</form>
</body>

<script>
    function login() {
        $("#loginForm").validate({
            submitHandler: function (form) {
                doLogin();
            }
        });

    }

    function doLogin() {
        var inputPass = $("#password").val();
        var salt = g_password_salt;
        var str = "" + salt.charAt(0) + salt.charAt(2) + inputPass + salt.charAt(5) + salt.charAt(4);
        var loginVo = {};
        loginVo.mobile = $("#mobile").val();
        loginVo.password = md5(str);
        $.ajax({
            url: "/login/doLogin",
            type: "POST",
            data: $.toJSON(loginVo),
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
                if (data.code == 200) {
                    console.log("成功");
                    window.location.href="/goods/toList"
                } else {
                    console.error(data.message);
                }
            },
            error: function () {

            }
        })
    }
</script>
</html>